<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- react的根元素 插入一个根组件 -->
    <div id="example"></div>
</body>
</html>
<script src="./js/react.development.js"></script>
<script src="./js/react-dom.development.js"></script>
<script src="./js/babel.js"></script>

<script type="text/babel">
    class Son extends React.Component
    {
        constructor(props)
        {
            super(props)

            this.state = {
                sondata: 'hello son'
            }

            this.handleClick = this.handleClick.bind(this)
        }

        handleClick()
        {
            //调用父组件的自定义事件
            this.props.getData(this.state.sondata)
        }

        render()
        {
            return(
                <div>
                    <h4>子组件接收到的数据为：{this.props.data}</h4>
                    <button onClick={this.handleClick}>点击给父组件传值</button>
                </div>
            )
        }
    }

    class Father extends React.Component
    {
        constructor(props)
        {
            super(props)

            this.state = {
                data: 'hello world'
            }

            this.getData = this.getData.bind(this)
        }

        getData(data)
        {
            console.log(`子元素传递过来的数据为：${data}`)
        }

        render()
        {
            return(
                <div>
                    <h2>父传子用属性</h2>
                    <h2>子传父用事件</h2>
                    <Son data={this.state.data} getData={this.getData} />
                </div>
            )
        }
    }

    ReactDOM.render(
        <Father />,
        document.getElementById('example')
    )
</script>